﻿using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Text;

using ZP.Data;
using ZP.Service;
using System.Collections.Generic;
using System.Collections.Specialized;
using Kwan.Data;
using System;
using System.Diagnostics;

namespace ZP.Web
{
    public partial class AdvanceSearch : System.Web.UI.Page
    {
        public string queryString = string.Empty;
        public string userID = string.Empty;
        public string rid = string.Empty;
        
        protected void Page_Load(object sender, EventArgs e)
        {
            NameValueCollection ps = new NameValueCollection(Request.Params);

            Page.Title += "职位检索 - ";

            userID = UserUtil.GetUserCookie("id");
            if (userID != null)
            {
                TUser u = DBContext.Load<TUser>(userID);
                if (u != null) rid = u.ResumeID.ToString();
            }

            queryString = HttpUtility.UrlDecode(Request.Form.ToString());

            Region r = new Region(0, 7);
            IList<VArticle> ArtList = new Expression().List<VArticle>(r);
            foreach (VArticle art in ArtList)
                Literal2.Text = Literal2.Text + "<li><a href='/arts/" + art.EntityID + ".html' target='_blank' title='" + art.Title + "'>[" + art.ClassName + "]" + art.Title + "</a></li>";
            Stopwatch sw = new Stopwatch(); sw.Start();
            JobInit(ps);
            sw.Stop();
            Response.Write(sw.ElapsedMilliseconds);
        }

        private void InitAD()
        {
            //ADMgr adMgr = new ADMgr();
            //List<TAD> adList = adMgr.GetADList(2);
            //if (adList.Count > 0)
            //{
            //    TAD ad = adMgr.GetAD(adList, "300*250");
            //    AD_300_250.Text = ad != null ? ad.Memo : "";
            //}
            //adMgr.Dispose();
        }

        private void JobInit(NameValueCollection ps)
        {
            ps.Set("Scale", "1");
            Expression exp = JobQuery.MakeSearchWhere(ps);

            IList<VJob> JobList = exp.List<VJob>(new Region(0, 15));
            long tbCount = exp.Count<TJob>();

            if (!String.IsNullOrEmpty(ps["jobarea"]))
            {
                HtmlInputHidden jobarea = (HtmlInputHidden)this.FindControl("jobarea");
                jobarea.Value = ps["jobarea"];
            }
            if (!String.IsNullOrEmpty(ps["funtype"]))
            {
                HtmlInputHidden funtype = (HtmlInputHidden)this.FindControl("funtype");
                funtype.Value = ps["funtype"];
            }
            if (!String.IsNullOrEmpty(ps["industrytype"]))
            {
                HtmlInputHidden industrytype = (HtmlInputHidden)this.FindControl("industrytype");
                industrytype.Value = ps["industrytype"];
            }

            string str = null;
            //int tbCount = tbJob.Rows.Count;int i=tbCount;
            
            long i = tbCount;
            if (tbCount > 20) i = 20;
            string jHtml = "<tr><td colspan='6' style='height:2px;'></td></tr>" +
"<tr class='tr0' bgcolor='#ffffff'>" +
    "<td class='td0'><input type='checkbox' name='check_XNumber' value='XNumber' ></td>" +
    "<td class='td1'><a href='/job/XNumber.html' class='jobname' target='_blank' >XJobName</a></td>" +
    "<td class='td2'><a href='/company/XCompanyID_1_10.html' class='coname' target='_blank' >XCompanyName</a></td>" +
    "<td class='td3'><span id='map_jobareaXNumber'>XCity</span></td>" +
    "<td class='td4'><span id='map_fbrqXNumber'>XIssuedate</span></td>" +
    "<td class='td5' valign='top'><div id='Appli_XNumber' onclick='app.appSingleJob(this);' class='btn_appjob' onmouseover=\"this.className='btn_appjobON'\" onmouseout=\"this.className='btn_appjob'\">立即申请</div></td>" +
"</tr><tr class='tr1'  bgcolor='#ffffff'>" +
    "<td class='td0'>&nbsp;</td>" +
    "<td colspan='4' class='td1234'>学历要求：XEducation&nbsp;&nbsp;|&nbsp;&nbsp;工作经验：XWorkYears &nbsp;&nbsp;|&nbsp;&nbsp;招聘人数：XEnrollments人</td>" +
    "<td class='td5'><div id='Fav_XNumber' onclick='app.favJob(this);' class='btn_favjob' onmouseover=\"this.className='btn_favjobON'\" onmouseout=\"this.className='btn_favjob'\">收藏</div><a id='Fav_XNumber' href='javascript:;' onclick='app.favJob(this);' style='color:#FF7300;'></td>" +
"</tr><tr class='tr2'  bgcolor='#ffffff'>" +
    "<td class='td0'>&nbsp;</td>" +
    "<td colspan='4' class='td1234 wordBreakNormal'>职位简介:<span >XDescription</span></td>" +
"</tr><tr class='tr3'><td colspan='6'></td></tr>";
            totalCount.Text = "共有 " + tbCount.ToString() + " 条记录";
            string[] Workyear = { "不限", "应届毕业生", "一年以上", "两年以上", "三年以上", "五年以上", "八年以上", "十年以上" };
            string[] Degree = { "不限", "初中", "高中", "中技", "中专", "大专", "本科", "硕士", "博士" };
            for (int j = 0; j < JobList.Count; j++)
            {
                string Enrollments = JobList[j].Enrollments == "0" ? "若干" : JobList[j].Enrollments;
                string Description = !String.IsNullOrEmpty(JobList[j].Description) ? Kwan.Helper.Strings.Format.RemoveHtml(JobList[j].Description) : JobList[j].Description;
                Description = Description.Length > 200 ? Description.Substring(0, 200) : Description;

                string jStr = jHtml;
                jStr = jStr.Replace("XNumber", JobList[j].EntityID.ToString());
                jStr = jStr.Replace("XCompanyID", JobList[j].CompanyID.ToString());
                jStr = jStr.Replace("XJobName", JobList[j].JobName);
                jStr = jStr.Replace("XCity", JobList[j].City);
                jStr = jStr.Replace("XIssuedate", JobList[j].EndDate.ToString("yyyy-MM-dd"));
                jStr = jStr.Replace("XEducation", Degree[int.Parse(JobList[j].Degreefrom)]);
                jStr = jStr.Replace("XWorkYears", Workyear[int.Parse(JobList[j].Workyear)]);
                jStr = jStr.Replace("XDescription", Description);
                jStr = jStr.Replace("XCompanyName", JobList[j].CompanyName);
                jStr = jStr.Replace("XEnrollments", Enrollments);
                //jStr = jStr.Replace("XWorkers", tbCmp.Rows[0]["Workers"].ToString());
                str += jStr;
            }
            ltrJobList.Text = "<tr class='titleTr'><td  class='td0'>&nbsp;</td><td  class='td1'>职位名称</td><td  class='td2'>公司名称</td><td  class='td3'>工作地点</td><td  class='td4'>结束日期</td><td  class='td5'>&nbsp;</td></tr>" + str;


            //page
            long page = tbCount / 20;
            if (page > 5) page = 5;
            //Response.Write(sql + page);
            string pages = null;
            string PageStr = "<table  class='searchPageNav'><tr>XLeftXPagesXRight</tr></table>";
            PageStr = PageStr.Replace("XLeft", "<td style='padding-right:5px;' ><img src='Images/pageloff.gif' border='0' /></td><td  class='currPage' >1</td>");
            for (int k = 2; k <= page; k++)
            {
                pages += "<td><a href='#" + k + "' class='orange1' onclick=app.jumpp(" + k + ") >" + k + "</a></td>";
            }
            PageStr = PageStr.Replace("XPages", pages);
            if (page < 2)
                PageStr = PageStr.Replace("XRight", "<td><img src='Images/pageroff.gif' style='border:0px; width:auto;margin-left:5px;'/></td>");
            else
                PageStr = PageStr.Replace("XRight", "<td><a href='#2' class='orange1' onclick='app.jumpp(2);return false;' style='border:0px; width:auto;margin-left:5px;' ><img src='/Images/pageron.gif' border='0' /></a></td>");
            PageLiteral.Text = PageStr;
        }

        private string ReadHtml(string fileName)
        {
            Encoding code = Encoding.GetEncoding("gb2312");
            // 读取模板文件
            string temp = HttpContext.Current.Server.MapPath("~\\AdvanceSearch\\" + fileName + ".htm");
            StreamReader sr = null;
            string str = "";
            try
            {
                sr = new StreamReader(temp, code);
                str = sr.ReadToEnd(); // 读取文件 
            }
            catch (Exception exp)
            {
                HttpContext.Current.Response.Write(exp.Message);
                HttpContext.Current.Response.End();
                sr.Close();
            }
            return str;
        }

        private void WriteCookies(string cookieName,string cookieValue)
        {
            HttpCookie myCookie = new HttpCookie("indexSearch");
            //DateTime dtNow = DateTime.Now;
            //TimeSpan tsMinute = new TimeSpan(0, 1, 0, 0);
            //myCookie.Expires = dtNow + tsMinute;
            myCookie[cookieName] = cookieValue;
            //加入此cookie
            Response.Cookies.Add(myCookie);
        }
    }
}
